package com.woniuxy.cloud.carl.marketing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.marketing.client.param.AddAdParam;
import com.woniuxy.cloud.carl.marketing.client.param.DelAdParam;
import com.woniuxy.cloud.carl.marketing.client.param.GetAdParam;
import com.woniuxy.cloud.carl.marketing.client.result.AdvertisementDto;
import com.woniuxy.cloud.carl.marketing.common.AdTypeEnum;
import com.woniuxy.cloud.carl.marketing.infra.model.AdItem;
import com.woniuxy.cloud.carl.marketing.infra.model.Advertisement;
import com.woniuxy.cloud.carl.marketing.infra.mapper.AdvertisementMapper;
import com.woniuxy.cloud.carl.marketing.service.AdItemService;
import com.woniuxy.cloud.carl.marketing.service.AdvertisementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.CommonResultCodeEnum;
import com.woniuxy.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 广告 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements AdvertisementService {
    @Autowired
    AdItemService adItemService;
    @Override
    public Result addAd(AddAdParam param) {
        if (param.getStartTime().compareTo(param.getEndTime())>0)
            throw new MallException(CommonResultCodeEnum.ILLEGAL_STATE,"开始时间不能大于结束时间");
        AdTypeEnum adTypeEnum = AdTypeEnum.getAdTypeEnum(param.getAdType());
        Advertisement advertisement = BeanUtil.copyProperties(param, Advertisement.class);
        save(advertisement);
        AdItem adItem = new AdItem();
        adItem.setDiscount(param.getDiscount());
        adItem.setImage(param.getImage());
        for (Long id : param.getIds()) {
            switch ((adTypeEnum)){
                case GOODS_AD:{
                    adItem.setSpuId(id);
                    break;
                }
                case SERVICE_AD:{
                    adItem.setServiceTypeId(id);
                    break;
                }
                case STORE_AD:{
                    adItem.setStoreId(id);
                    break;
                }
                default:{
                    break;
                }
            }
            adItemService.save(adItem);
            adItem.setAdItemId(null);
        }

        return Result.success(CommonResultCodeEnum.SUCCESS,"添加活动广告成功",null);
    }

    @Override
    public Result delAd(DelAdParam param) {
        for (Long adId : param.getAdIds()) {
            removeById(adId);
            adItemService.remove(new QueryWrapper<AdItem>().eq("ad_id",adId));
        }
        return Result.success();
    }

    @Override
    public Result<List<AdvertisementDto>> getAd(GetAdParam param) {
        ArrayList<AdvertisementDto> advertisementDtos = new ArrayList<>();
        List<Advertisement> advertisements=new ArrayList<>();
        if (param.getAdType()!=null) {
            AdTypeEnum adTypeEnum = AdTypeEnum.getAdTypeEnum(param.getAdType());
            if (adTypeEnum==null) throw new MallException(CommonResultCodeEnum.DATA_NOT_FOUND,"该类型不存在");
            advertisements = list(new QueryWrapper<Advertisement>()
                    .eq("ad_type", param.getAdType()));
        } else if (param.getAdIds()!=null) {
            advertisements = listByIds(param.getAdIds()).stream().collect(Collectors.toList());
        }else {
            advertisements=list();
        }
        for (Advertisement advertisement : advertisements) {
            AdvertisementDto advertisementDto = BeanUtil.copyProperties(advertisement, AdvertisementDto.class);
            List<AdItem> adItems = adItemService.list(new QueryWrapper<AdItem>().
                    eq("ad_id", advertisement.getAdId()));
            if (adItems!=null) {
                advertisementDto.setImage(adItems.get(0).getImage());
                advertisementDto.setDiscount(adItems.get(0).getDiscount());
            }
            AdTypeEnum adTypeEnum = AdTypeEnum.getAdTypeEnum(advertisement.getAdType());
            switch (adTypeEnum){
                case STORE_AD:{
                    List<Long> longList = adItems.stream().map(o -> o.getStoreId()).collect(Collectors.toList());
                    advertisementDto.setStoreIds(longList);
                    advertisementDtos.add(advertisementDto);
                    break;
                }
                case GOODS_AD:{
                    List<Long> longList = adItems.stream().map(o -> o.getSpuId()).collect(Collectors.toList());
                    advertisementDto.setSpuIds(longList);
                    advertisementDtos.add(advertisementDto);
                    break;
                }
                case SERVICE_AD:{
                    List<Long> longList = adItems.stream().map(o -> o.getServiceTypeId()).collect(Collectors.toList());
                    advertisementDto.setServiceIds(longList);
                    advertisementDtos.add(advertisementDto);
                    break;
                }
                default:{
                    break;
                }
            }
        }
        return Result.success(advertisementDtos);
    }
}
